from selenium import webdriver
import  time
import requests
import json
import base64

browser = webdriver.Chrome('C:\\Users\\Administrator\\www\\chromedriver.exe')

# 获取验证码图片
browser.get('http://127.0.0.1:8080/register')
time.sleep(3)

img = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/form/table/tr[4]/td/img')
img.screenshot('res.png')

token_str = requests.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=VQTpvznDiwGFYGeFWs5sESGb&client_secret=riOO9fQSsqNL3zcEYIBdahd8sDve6F2p").text

# 准备请求百度智能云
# token参数
token = json.loads(token_str)['access_token']
#请求头参数
header = {
    'Content-Type':"application/x-www-form-urlencoded"
}

# 要识别的图片
f = open('./res.png','rb')
img = base64.b64encode(f.read())
data = {'image':img}

# 请求的url
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"+'?access_token='+token
# 发起请求
response = requests.post(request_url,data=data,headers=header)
# 获取到code
code = response.json()["words_result"][0]['words']


username = browser.find_element_by_xpath("//*[@id='app']/div/section/div/form/table/tr[1]/td/input")
password = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/form/table/tr[2]/td/input')
codeword = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/form/table/tr[3]/td/input')
sub = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/form/table/tr[5]/td/button[1]')

username.send_keys('tt')
password.send_keys('123')
codeword.send_keys(code)
sub.click()

time.sleep(3)

browser.close()
